<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新闻日期">
          <a-date-picker placeholder="请输入新闻日期" v-decorator="['NewsDate', validatorRules.newsdate]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新闻类型">
          <a-select placeholder="请选择新闻类型" v-decorator="['NewsTypeId', validatorRules.newstypeid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in newsTypeData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新闻标题">
          <a-input type="textarea" placeholder="请输入新闻标题" v-decorator="['NewsTitle', validatorRules.newstitle]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="摘要">
          <a-input type="textarea" placeholder="请输入摘要" v-decorator="['Remarks', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="链接地址">
          <a-input placeholder="请输入链接地址" v-decorator="['Url', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="新闻来源">
          <a-select placeholder="请输入新闻来源" v-decorator="['DataSourceId', validatorRules.datasourceid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.BaseInfoId" v-for="(item,index) in dataSourceData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        
		
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyNewsModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        companyData:[],
        dataSourceData:[{
          id:'158',
          name:'政府授权'
        },{
          id:'159',
          name:'企业授权'
        },{
          id:'160',
          name:'第三方机构授权'
        }],
        newsTypeData:[{
          id:'83',
          name:'投融资'
        },{
          id:'84',
          name:'IPO并购'
        },{
          id:'85',
          name:'独角兽'
        },{
          id:'86',
          name:'商业合作'
        },{
          id:'87',
          name:'政策'
        },{
          id:'88',
          name:'人事变更'
        },{
          id:'89',
          name:'新品发布'
        },{
          id:'90',
          name:'产品获批'
        },{
          id:'91',
          name:'业绩发布'
        },{
          id:'92',
          name:'报告'
        },{
          id:'93',
          name:'盘点'
        },{
          id:'94',
          name:'科研进展'
        },{
          id:'95',
          name:'人物观点'
        },{
          id:'96',
          name:'商业纠纷'
        },{
          id:'97',
          name:'公司动态'
        },{
          id:'98',
          name:'其它'
        }],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companynewsid:{rules: [{ required: true, message: '请输入新闻情报ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        newsdate:{rules: [{ required: true, message: '请输入新闻日期!' }]},
        newstypeid:{rules: [{ required: true, message: '请选择新闻类型!' }]},
        newstitle:{rules: [{ required: true, message: '请输入新闻标题!' }]},
        remarks:{rules: [{ required: true, message: '请输入摘要!' }]},
        url:{rules: [{ required: true, message: '请输入链接地址!' }]},
        datasourceid:{rules: [{ required: true, message: '请输入新闻来源!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        url: {
          add: "/companyNews/add",
          edit: "/companyNews/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      this.getCompany();
      this.getBaseData();
    },
    methods: {
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'NewsType'
            }]}).then((res)=>{
          if(res.status == 1){
            this.newsTypeData = res.data.rows;
          }
        })
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'DataSources'
            }]}).then((res)=>{
          if(res.status == 1){
            this.dataSourceData = res.data.rows;
          }
        })
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.CompanyId = this.model.Company;
        this.model.NewsTypeId = this.model.NewsType;
        this.model.DataSourceId = this.model.DataSource;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'CompanyId','NewsDate','NewsTypeId','NewsTitle','Remarks','Url','DataSourceId','Company_CompanyName'))
		  //时间格式化
      this.form.setFieldsValue({NewsDate:this.model.NewsDate?moment(this.model.NewsDate,'YYYY-MM-DD HH:mm:ss'):null})
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyNewsId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.NewsDate = formData.NewsDate?formData.NewsDate.format('YYYY-MM-DD HH:mm:ss'):null;
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>